let appCityPopup = getApp();
Component({
  data: {
    hidden: false,
    classVisible: false,
    cityList: <any[]>[],
    currentCity: '',
    selectCityIndex: 0,
    themeCode:''
  },
  properties: {
    visible: {
      type: Boolean,
      value: false,
      observer(newVal, oldVal) {
        if (newVal) {
          let index = -1
          console.log(appCityPopup.globalData.cityList)
          appCityPopup.globalData.cityList.forEach((item: any, i: number) => {
            if (item.city === appCityPopup.globalData.currentCity) {
              index = i
            }
          })
          this.setData({
            hidden: newVal,
            cityList: [...appCityPopup.globalData.cityList],
            currentCity: wx.getStorageSync('userInitialCity'),
            selectCityIndex: index,
            themeCode: wx.getStorageSync('themeCode')
          })
          setTimeout(() => {
            this.setData({
              classVisible: newVal
            })
          }, 30)
        } else {
          this.setData({
            classVisible: newVal
          })
          setTimeout(() => {
            this.setData({
              hidden: newVal
            })
          }, 400)
        }
      }
    }
  },  
  lifetimes: {
  },
  methods: {
    onClosePopup() {
      // detail对象，提供给事件监听函数
      let myEventDetail = {
        visible: !this.data.hidden,
        city: this.data.cityList[this.data.selectCityIndex].city,
        cityId: this.data.cityList[this.data.selectCityIndex].cityId,
      };
      appCityPopup.globalData.currentCity = myEventDetail.city
      let myEventOption = {}; // 触发事件的选项
      this.triggerEvent("change", myEventDetail, myEventOption);
    },
    onSelectCityIndex(res: any) {
      this.setData({
        selectCityIndex: res.currentTarget.dataset.index
      })
      this.onClosePopup()
    }
  }
})